/*
 * testserver.cpp
 *
 *  Created on: Apr 14, 2011
 *      Author: Alan Mattila
 */

#include <iostream>
#include <cassert>

#include "testserver.h"
#include "../../../board/Board.h"
#include "../../../checkers/Checkers.h"
#include "../../NetworkServer.h"
#include "../../../misc/debug.h"

using std::cout;

testserver::testserver(QWidget *parent = 0)
{
	closeButton = NULL;
	startButton = NULL;
	checkers = NULL;
	server = NULL;
	tcpSocket = NULL;

	// Set up the GUI
	initWindow();

	TRACE("\n");
	checkers = new Checkers();
	server = new NetworkServer(checkers);
	assert(checkers != NULL);
	assert(server != NULL);
}

testserver::~testserver()
{
	if(checkers)
		delete checkers;

	if(server)
		delete server;

	if(closeButton)
		delete closeButton;

	if(startButton)
		delete startButton;

	if(moveButton)
		delete moveButton;
}


void testserver::initWindow(void)
{
	this->setFixedSize(300, 200);

	// Create start hosting button
	startButton = new QPushButton(QString("Start Hosting"), this);
	startButton->setGeometry(20, 40, 100, 30);
	startButton->show();
	connect(startButton, SIGNAL(clicked()), this, SLOT(StartHosting()));

	// Create close button
	closeButton = new QPushButton(QString("Close"), this);
	closeButton->setGeometry(140, 40, 100, 30);
	closeButton->show();
	connect(closeButton, SIGNAL(clicked()), this, SLOT(Close()));

	// create the Move button
	moveButton = new QPushButton(QString("Move"), this);
	moveButton->setGeometry(20, 100, 100, 30);
	moveButton->show();
	connect(moveButton, SIGNAL(clicked()), this, SLOT(TestMove()));
}


void testserver::StartHosting(void)
{
	TRACE("Start: testserver::StartHosting()\n");
	// Start hosting the game
	server->StartHosting();

	// Connect signals
	tcpSocket = server->GetTcpSocket();
	TRACE("End: testserver::StartHosting()\n");
}


void testserver::Close(void)
{
	close();
}


void testserver::Error(void)
{
	TRACE("");
	qDebug() << "Received error() signal!";
	qDebug() << "  " << tcpSocket->errorString();
}


void testserver::BytesWritten(void)
{
	TRACE("");
	qDebug() << "Received bytesWritten() signal!";
}


void testserver::Disconnected(void)
{
	TRACE("");
	qDebug() << "Received disconnected() signal!";
}


void testserver::TestMove(void)
{
	if(checkers->Move(0,5,1,4) == MOVE_FAIL)
		TRACE("Move failed\n");
	server->MakeMove();
}

